package br.ufes.inf.lprm.cwtools.tools;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;

@Component
@ApplicationScoped
public class JPAUtil {
	private EntityManager em;
	private EntityManagerFactory emf;

	public EntityManager getEntityManager() {
		if (emf == null){
			emf = Persistence.createEntityManagerFactory("gwa");
		}
		if (em == null){
			em = emf.createEntityManager();
		}
		return em;
	}
	
	public void closeEntityManager() {
		if (em != null){
			em.close();
			em = null;
		}
		if (emf != null){
			emf.close();
		}
	}
}
